﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_304_09
    {
        public static void ISYC_304_F44(Dictionary<string, Object> data)  //圆角铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            /*double DCX = 40.33;
            double DHUB = 13.63;
            double DCONMS = 8.65;
            double FDP = 25.54;
            double DCCB = 9.92;
            double LCCB = 3.79;
            double CW = 9.49;
            double OAL = 11.52;
            double DC = 34;
            double APMX = 4.79;
            double CPRADZ = 12.45;
            double CPRADX = 21.74;*/

            double DCX = Convert.ToDouble(data["dcx"]);
            double DHUB = Convert.ToDouble(data["dhub"]);
            double DCONMS = Convert.ToDouble(data["dconms"]);
            double FDP = Convert.ToDouble(data["fdp"]);
            double DCCB = Convert.ToDouble(data["dccb"]);
            double LCCB = Convert.ToDouble(data["lccb"]);
            double CW = Convert.ToDouble(data["cw"]);
            double OAL = Convert.ToDouble(data["oal"]);
            double DC = Convert.ToDouble(data["dc"]);
            double APMX = Convert.ToDouble(data["apmx"]);
            double CPRADZ = Convert.ToDouble(data["cpradz"]);
            double CPRADX = Convert.ToDouble(data["cpradx"]);

            double scale = 30.36 / OAL;

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //中心线
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, 0 * pi / 180, OAL);

            //轮廓
            Vector2 v3 = fun.Polar(v1, 90 * pi / 180, DHUB / 2.0);
            Vector2 v4 = fun.Polar(v3, 0 * pi / 180, OAL - CW);
            Vector2 v5 = fun.Polar(v1, 0 * pi / 180, OAL - CW);
            Vector2 v6 = fun.Polar(v5, 90 * pi / 180, DCX / 2.0);
            Vector2 v7 = fun.Polar(v6, 0 * pi / 180, CW - APMX);
            Vector2 v8 = fun.Polar(v2, 90 * pi / 180, DC / 2.0);
            Vector2 v9 = fun.Polar(v8, -90 * pi / 180, (0.81 / 34) * DC);
            Vector2 v10 = fun.Polar(v7, -90 * pi / 180, (6.33 / 34) * DC);
            Vector2 v11 = fun.Polar(v9, -90 * pi / 180, (3.8 / 34) * DC);
            Vector2 v12 = fun.Polar(v10, -90 * pi / 180, (1.45 / 34) * DC);
            Vector2 v13 = fun.Polar(v11, -90 * pi / 180, (1.44 / 34) * DC);
            Vector2 v14 = fun.Polar(v12, -90 * pi / 180, (4.9 / 34) * DC);
            Vector2 v15 = fun.Polar(v13, -90 * pi / 180, (4.01 / 34) * DC);
            Vector2 v16 = fun.Polar(v14, -90 * pi / 180, (1.53 / 34) * DC);
            Vector2 v17 = fun.Polar(v15, -90 * pi / 180, (1.58 / 34) * DC);
            Vector2 v18 = fun.Polar(v16, -90 * pi / 180, (5.13 / 34) * DC);
            Vector2 v19 = fun.Inters(v18, fun.Polar(v18, 0 * pi / 180, OAL), v2, fun.Polar(v2, 90 * pi / 180, DC));
            Vector2 v20 = new Vector2(v3.X, -v3.Y);
            Vector2 v21 = new Vector2(v4.X, -v4.Y);
            Vector2 v22 = new Vector2(v6.X, -v6.Y);
            Vector2 v23 = new Vector2(v7.X, -v7.Y);
            Vector2 v24 = new Vector2(v8.X, -v8.Y);
            Vector2 v25 = new Vector2(v1.X, v1.Y - DCONMS / 2.0);
            Vector2 v26 = new Vector2(v2.X - LCCB, v25.Y);
            Vector2 v27 = new Vector2(v2.X - LCCB, v2.Y);
            Vector2 v28 = new Vector2(v26.X, v2.Y - DCCB / 2.0);
            Vector2 v29 = new Vector2(v2.X, v2.Y - DCCB / 2.0);
            Vector2 v30 = new Vector2(v20.X + CPRADZ, v2.Y - CPRADX);
            Vector2 v31 = new Vector2(v2.X - APMX, v2.Y);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //中心线
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.Center;
            line1.LinetypeScale = 5;
            dxf.AddEntity(line1);

            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            fun.PolyFill(dxf, polyline1, 0.3, contour);

            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v5));
            polyline2.Vertexes.Add(new LwPolylineVertex(v6));
            polyline2.Vertexes.Add(new LwPolylineVertex(v7));
            polyline2.Vertexes.Add(new LwPolylineVertex(v8));//2
            polyline2.Vertexes.Add(new LwPolylineVertex(v24));
            polyline2.Vertexes.Add(new LwPolylineVertex(v23));//4
            polyline2.Vertexes.Add(new LwPolylineVertex(v22));
            polyline2.Vertexes.Add(new LwPolylineVertex(v21));
            polyline2.Vertexes.Add(new LwPolylineVertex(v20));
            polyline2.Vertexes.Add(new LwPolylineVertex(v1));
            polyline2.Vertexes[2].Bulge = Math.Tan(80 * pi / 180 / 4);
            polyline2.Vertexes[4].Bulge = Math.Tan(80 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v7));
            polyline3.Vertexes.Add(new LwPolylineVertex(v9));//0
            polyline3.Vertexes[0].Bulge = Math.Tan(91 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v10));
            polyline4.Vertexes.Add(new LwPolylineVertex(v11));//0
            polyline4.Vertexes[0].Bulge = Math.Tan(46 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline4, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v12));
            polyline5.Vertexes.Add(new LwPolylineVertex(v13));//0
            polyline5.Vertexes[0].Bulge = Math.Tan(54 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v14));
            polyline6.Vertexes.Add(new LwPolylineVertex(v15));//0
            polyline6.Vertexes[0].Bulge = Math.Tan(30 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v16));
            polyline7.Vertexes.Add(new LwPolylineVertex(v17));//0
            polyline7.Vertexes[0].Bulge = Math.Tan(26 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v18));
            polyline8.Vertexes.Add(new LwPolylineVertex(v19));
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            LwPolyline polyline9 = new LwPolyline();
            polyline9.Vertexes.Add(new LwPolylineVertex(v7));
            polyline9.Vertexes.Add(new LwPolylineVertex(v31));
            fun.PolyFill(dxf, polyline9, 0.3, contour);

            LwPolyline polyline10 = new LwPolyline();
            polyline10.Vertexes.Add(new LwPolylineVertex(v25));
            polyline10.Vertexes.Add(new LwPolylineVertex(v26));
            fun.PolyFill(dxf, polyline10, 0.3, contour);

            LwPolyline polyline11 = new LwPolyline();
            polyline11.Vertexes.Add(new LwPolylineVertex(v27));
            polyline11.Vertexes.Add(new LwPolylineVertex(v28));
            polyline11.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, polyline11, 0.3, contour);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v23));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v21));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v20));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            /*fun.Axis(dxf, v1, 180, 270, "", "", "PCS", 0.1 * scale, height * 0.5, 0);
            fun.Axis(dxf, v2, 180, 270, "", "", "CIP", 0.1 * scale, height * 0.5, 0);
            fun.Axis(dxf, fun.Polar(v20, -120 * pi / 180, 19.36 * scale), 180, 270, "Z", "Y", "X", 0.1 * scale, height, 0);*/

            //直线标注
            //DCONMS
            fun.dimrot1(dxf, new Vector2(v1.X, v1.Y + DCONMS / 2.0), v25, 90, OAL * 0.5, 0.1, height, arrowsize, exline, "DCONMS", "", "");

            //DHUB
            fun.dimrot1(dxf, v3, v20, 90, OAL, 0.1, height, arrowsize, exline, "DHUB", "", "");

            //OAL
            fun.dimrot1(dxf, v3, v8, 0, DC * 0.8, 0.1, height, arrowsize, exline, "OAL", "", "");

            //CW
            fun.dimrot1(dxf, v6, v8, 0, DC * 0.4, 0.1, height, arrowsize, exline, "CW", "", "");

            //APMX
            //fun.dimrot1(dxf, v7, v8, 0, DC * 0.2, 0.1, height, arrowsize, exline, "APMX", "", "");

            //DCX
            //fun.dimrot1(dxf, v7, v23, -90, OAL * 4, 0.1, height, arrowsize, exline, "DCX", "", "");

            //DC
            fun.dimrot1(dxf, v8, v24, -90, OAL * 3, 0.1, height, arrowsize, exline, "DC", "", "");

            //DCCB
            fun.dimrot1(dxf, new Vector2(v2.X, v2.Y + DCCB / 2.0), v29, -90, OAL * 2, 0.1, height, arrowsize, exline, "DCCB", "", "");

            //CPRADX
            //fun.dimrot1(dxf, v2, v30, -90, OAL * 1, 0.1, height, arrowsize, exline, "CPRADX", "", "");

            //CPRADZ
            //fun.dimrot1(dxf, v20, v24, 180, DC * 0.8, 0.1, height, arrowsize, exline, "CPRADZ", "", "");

            //LCCB
            fun.dimrot1(dxf, v28, v24, 180, DC * 0.6, 0.1, height, arrowsize, exline, "LCCB", "", "");

            //箭头
            //FDP
            //fun.jt1(dxf, fun.Polar(v8, 50 * pi / 180, (11.45 / 30.36) * DC), -90, 5, 1, "FDP", height);

            //CCMS
            //fun.jt1(dxf, fun.Polar(v25, 180 * pi / 180, (3.26 / 9.49) * CW), -119, 8, 5 * 0.7, "CCMS", height);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
